package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends WebDriverHelperBase{

	private static boolean CREATION = true;
	private static boolean MODIFICATION = false;
	
	public ContactHelper createContact(ContactData contact) {
		manager.navigateTo().newContactPage();
		fillContactForm(contact, ContactHelper.CREATION);
		submitContactCreation();
		manager.navigateTo().returnToHomePage();
		manager.getModel().addContact(contact);
		return this;
	}

	public ContactHelper removeContact(int index) {
		manager.navigateTo().mainPage();
		initContactRemoval(index);
		submitContactDeletion();
		manager.navigateTo().returnToHomePage();
		manager.getModel().removeContact(index);
		return this;
	}

	public ContactHelper modifyContact(int index, ContactData contact) {
		manager.navigateTo().mainPage();
		initContactEditable(index);
		fillContactForm(contact, ContactHelper.MODIFICATION);
		submitContactUpdating();
		manager.navigateTo().returnToHomePage();		
		manager.getModel().removeContact(index).addContact(contact);
		return this;
	}

	public SortedListOf<ContactData> getUiContacts() {
		manager.navigateTo().mainPage();
		SortedListOf<ContactData> contacts = new SortedListOf<ContactData>(); 
		List<WebElement> rows = driver.findElements(By.xpath("//tr[@name='entry']"));
		
		for (int i=0;i<rows.size();i++) {
			ContactData contact = new ContactData()
				.withFirstName(rows.get(i).findElement(By.xpath("./td[3]")).getText())
				.withLastName(rows.get(i).findElement(By.xpath("./td[2]")).getText())
				.withPhoneOnMainPage(rows.get(i).findElement(By.xpath("./td[5]")).getText());
			
			contacts.add(contact);
		}	
		
		return contacts;
	}

	// --------------------------------------------------------------------------------------------
	
	public ContactHelper(AppManager manager) {
		super(manager);
	}

	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		return this;
	}

	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type(By.name("firstname"), contact.getFirstName());
		type(By.name("lastname"), contact.getLastName());
		type(By.name("address"), contact.getAddress());
		type(By.name("home"), contact.getPhoneHome());
		type(By.name("mobile"), contact.getPhoneMobile());
		type(By.name("work"), contact.getPhoneWork());
		type(By.name("email"), contact.getEmail1());
		type(By.name("email2"), contact.getEmail2());
		selectByIdItem(contact.getDayOfBirth(), By.name("bday"));
		selectByIdItem(contact.getMonthOfBirth(), By.name("bmonth"));
	    type(By.name("byear"), contact.getYearOfBirth());
	    if (formType == CREATION){
	    	//select(contact.getGroup(), By.name("new_group"));
	    }   else {
			if (driver.findElements(By.name("new_group")).size() != 0)
				throw new Error("Group selector exists on Contact Form");
		}
	    type(By.name("address2"), contact.getAddress2());
	    type(By.name("phone2"), contact.getPhoneHome2());
		return this;
	}
	

	public ContactHelper initContactRemoval(int index) {
		click(By.xpath("(//img[@alt='Edit'])["+(index+1)+"]"));
		return this;
	}
	
	public ContactHelper submitContactUpdating() {
		click(By.name("update"));
		return this;
	}
	
	public ContactHelper submitContactDeletion() {
		click(By.xpath("(//input[@name='update'])[2]"));
		return this;
	}

	public ContactHelper initContactEditable(int index) {
		initContactRemoval(index);
		return this;
	}

	public ContactHelper initContactView(int i) {
		click(By.xpath("(//img[@alt='Details'])[1]"));
		return this;
	}

	public ContactHelper modifyContact() {
		click(By.name("modifiy"));
		return this;
	}	
	
	public List<WebElement> getBday() {
		return driver.findElements(By.xpath("//select[@name='bday']/option[@value!='0']"));
	}
	
	public List<WebElement> getBmonth() {
		return driver.findElements(By.xpath("//select[@name='bmonth']/option[@value!='-']"));
	}
}
